import {
  Body,
  Controller,
  Delete,
  Get,
  HttpException,
  Param,
  Post,
  Put,
  Query,
} from "@nestjs/common";
import { ApiOperation, ApiResponse, ApiTags } from "@nestjs/swagger";
import { query } from "express";
import { APIResponse } from "src/common/decorators/APIResponse.decorators";
import { PageDto, ResOp } from "src/common/dto/ResOp.dto";
import { ListUserTodo, UpdateUserTodo, CreateUserTodo } from "./dto/user.dto";
import { UserService } from "./user.service";

@ApiTags("用户管理")
@Controller("user")
export class UserController {
  constructor(private readonly usersService: UserService) {}

  @Get("list")
  @ApiOperation({ summary: "用户列表" })
  @APIResponse([ListUserTodo], true)
  getAll(@Query("page") page?: number) {
    return this.usersService.findAll();
  }

  @Post("create")
  @ApiOperation({ summary: "新增用户" })
  @APIResponse(true)
  async create(@Body() CreateUserTodo: CreateUserTodo): Promise<Boolean> {
    return await this.usersService.create(CreateUserTodo);
  }

  @Put("edit")
  @ApiOperation({ summary: "编辑用户" })
  @APIResponse(true)
  async update(@Body() UserTodo: UpdateUserTodo): Promise<Boolean> {
    return await this.usersService.update(UserTodo.id, UserTodo);
  }

  @Get("info")
  @ApiOperation({ summary: "用户详情" })
  @APIResponse(ListUserTodo)
  async getOne(@Query("id") id: string): Promise<ListUserTodo> {
    // throw new HttpException("请求失败", 500);
    return await this.usersService.getOne(id);
  }

  @Delete("delete")
  @ApiOperation({ summary: "删除用户" })
  @APIResponse(true)
  async delete(@Query("id") id: string): Promise<Boolean> {
    return await this.usersService.delete(id);
  }
}
